#!/bin/bash
# 关闭 Bash 的历史扩展功能
set +H

# Get version from the command line argument, default to "3.1.6" if not provided
version="${1:-3.1.6}"
ssh_password="${2:-pasword}"

# Dynamically get the main IP address of the machine
# This command gets the IP address of the default network interface
main_ip=$(ip route get 1.2.3.4 | awk '{print $7}' | head -n 1)

# SSH连接信息写入到 /etc/ansible/hosts
mkdir -p /etc/ansible
cat <<EOF > /etc/ansible/hosts
[hyperxservers]
${main_ip} ansible_user=root ansible_ssh_pass=${ssh_password}
EOF

# Update package lists
sudo apt update -y

# 安装 sshpass
apt install sshpass -y

# Install ansible
apt install python3-pip -y
pip install ansible -i https://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com

# Download the HyperX package
cd /opt/
# wget "http://117.143.63.166:30095/installer/hyperx_${version}.tar.gz"
wget "https://hyperx-1330232465.cos.ap-shanghai.myqcloud.com/hyperx_${version}.tar.gz"

# Extract the package
tar zxvf "hyperx_${version}.tar.gz"

# Change directory to the HyperX packages folder
cd /opt/hyperx_packages

# Create the required directory structure
mkdir -p /opt/src/hyperx-devevops

# Extract the installer package into the desired directory
tar -zxvf installer.tar.gz -C /opt/src/hyperx-devevops

# Execute the Ansible playbook
ansible-playbook -i /etc/ansible/hosts -l hyperxservers \
      /opt/src/hyperx-devevops/installer/ansible/site.yml \
      --user root \
      -e "ansible_ssh_common_args='-o StrictHostKeyChecking=no'" \
      -e "nfs_server_ip=${main_ip}" \
      -e "download_package=local" \
      -e "version=${version}" \
      -e "ansible_user=root"